const mysql = require("mysql2");
const config = require("../config/index");

const connections = mysql.createPool({
  ...config.db,
});

// 测试数据库是否连接成功
connections.getConnection((err, conn) => {
  conn.connect((err) => {
    if (err) {
      throw new Error("数据库链接失败");
    } else {
      console.log("连接成功~");
    }
  });
});

const conn = connections.promise();

//根据id查询数据
const selectOne = (sql, params) => conn.query(sql, params);

//以对象形式添加一条数据
const insertOne = (sql, obj) => conn.query(sql, obj);

module.exports = {
  selectOne,
  insertOne,
};
